package com.unc.allergyex.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.unc.allergyex.R;

/**This is the expandable list adapter for the about activity. 
 * It uses static strings to populate its categories and items.
 * It is used by the About activity.  It uses the category.xml and
 * about_item.xml layout files.   
 * @author Stephanie
 */
public class AboutAdapter extends BaseExpandableListAdapter {

	//necessary for automatic methods
	private final int MAX_ITEMS_PER_CATEGORY = 2048;
	
	//used to make the indicator icon change drawables when the list is expanded or contracted
	private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
	private static final int[][] GROUP_STATE_SETS = {
        EMPTY_STATE_SET, // 0
        GROUP_EXPANDED_STATE_SET // 1
	};
	
	private ArrayList<String> categories;
	private ArrayList<ArrayList<String>> items;
	private LayoutInflater inflater;
	
	/**Constructor for the AboutAdapter.  
	 * @param context the context into which views should be inflated
	 * @param categories the ArrayList of Strings to use as category names
	 * @param items the ArrayList of ArrayLists of Strings to use as item content
	 */
	public AboutAdapter(Context context,
			 		    ArrayList<String> categories,
			 		    ArrayList<ArrayList<String>> items){
		this.items = items;
		this.categories = categories;
		this.inflater = LayoutInflater.from(context);
	}
	
	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChild(int, int)
	 */
	@Override
	public String getChild(int arg0, int arg1) {
		return items.get(arg0).get(arg1);
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChildId(int, int)
	 */
	@Override
	public long getChildId(int categoryIndex, int childIndex) {
		return (long) (categoryIndex*MAX_ITEMS_PER_CATEGORY + childIndex);
	}

	/**This method creates the child view when the user taps on the indicator icon for the category view. Uses the category.xml file.
	 * This method is automatically called by Android.  This method sets up the layout elements: makes the header text
	 * the item string.
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getChildView(int categoryIndex, int itemIndex,
			boolean isLastChild, View convertView, ViewGroup parent) {
		//inflate the view into the context
		View v;  
		if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.about_item, parent, false); 
		
		//set the textview to be the text of the appropriate child
		String text = getChild( categoryIndex, itemIndex );
		TextView name = (TextView)v.findViewById( R.id.content );
		if( name != null )
			name.setText( text );
		
        return v;
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
	 */
	@Override
	public int getChildrenCount(int categoryIndex) {
		return items.get( categoryIndex ).size();
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroup(int)
	 */
	@Override
	public Object getGroup(int categoryIndex) {
		return categories.get(categoryIndex);
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroupCount()
	 */
	@Override
	public int getGroupCount() {
		return categories.size();
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroupId(int)
	 */
	@Override
	public long getGroupId(int categoryIndex) {
		return (long) (categoryIndex*MAX_ITEMS_PER_CATEGORY);
	}

	/**Method to generate the view layout for a group header.  Uses the category.xml layout.
	 * Sets the header textview to have the categoryname.  Sets up a custom indicator icon.
	 * This method is automatically called by Android.
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		//inflate the view into the context
		View v = null;
	    if( convertView != null )
	    	v = convertView;
	    else
	    	v = inflater.inflate(R.layout.category, parent, false); 
	    
	    //set the textview to be the category string
	    String categoryName = (String) getGroup( groupPosition );
	    TextView category = (TextView)v.findViewById( R.id.categoryName );
		if( category != null )
			category.setText( categoryName );
		
		//set the behavior of the indicator icon
		ImageView ind = (ImageView) v.findViewById( R.id.explist_indicator);
		int stateSetIndex = ( isExpanded ? 1 : 0) ;
		Drawable drawable = ind.getDrawable();
		drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
	    return v;
	}

	/**Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		return true;
	}

	/** Auto-generated method required for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
	 */
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
}